﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net.Mail;
using System.Text.RegularExpressions;
namespace EDTS.Utils
{
    public class Email
    {
        public static bool SendEmail(string subject, string htmlContent, MailAddress fromEmail, MailAddressCollection toEmails, MailAddressCollection bccEmails = null)
        {
            MailMessage message = new MailMessage();
            message.Body = htmlContent;
            message.Subject = subject;
            message.IsBodyHtml = true;
            message.HeadersEncoding = ASCIIEncoding.UTF8;
            message.BodyEncoding = ASCIIEncoding.UTF8;
            message.SubjectEncoding = ASCIIEncoding.UTF8;

            if (fromEmail != null) message.From = fromEmail;
            if (toEmails != null)
            {
                foreach (MailAddress address in toEmails)
                {
                    message.To.Add(address);
                }
            }
            if (bccEmails != null)
            {
                foreach (MailAddress address in bccEmails)
                {
                    message.Bcc.Add(address);
                }
            }

            return SendEmail(message);


        }

        private static bool SendEmail(MailMessage message)
        {
            SmtpClient client = new SmtpClient();
            client.Send(message);
            return true;
        }

        public static bool IsEmail(string email)
        {
            if (string.IsNullOrEmpty(email)) return false;
            return Regex.IsMatch(email, @"^[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+$");
        }
    }
}
